﻿using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi
{
    /// <summary>
    /// 登录
    /// </summary>
    [CPPEI.WebApi.PostAction("SYS", "Login", "1.0")]
    public class LoginAction : BaseAction<LoginParams, LoginResult>
    {
        protected override ActionResult<LoginResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            LoginParams opParams)
        {
            LoginResult result = new LoginResult();
            result.UserId = opParams.Account;

            ServiceResult<string> serResult = new SER.AccountService().Login(opParams.Account, opParams.Password);
            if(serResult.IsSuccess)
            {
                Intergration.Config.AuthProvider.Save(result.UserId, TimeSpan.FromHours(1));
                return new ActionResult<LoginResult>(result);
            }

            return new ActionResult<LoginResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
        }
    }

    public class LoginParams
    {
        /// <summary>
        /// 账号
        /// </summary>
        public string Account;

        /// <summary>
        /// 密码
        /// </summary>
        public string Password { get; set; }
    }

    public class LoginResult
    {
        public string UserId { get; set; }
    }
}
